<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>客服配置</span>
      <el-button
        size="mini"
        type="success"
        class="btn-add"
        @click="handleAdd"
        style="margin-left: 20px"
        >添加客服</el-button
      >
    </el-card>
    <div class="table-container message-container">
      <el-table
        ref="adminTable"
        size="mini"
        :data="list"
        style="width: 100%;"
        v-loading="listLoading"
        border
      >
        <el-table-column label="ID" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="客服名称" align="center">
          <template slot-scope="scope">{{ scope.row.waiterName }}</template>
        </el-table-column>
        <el-table-column label="客服地址" align="center">
          <template slot-scope="scope">{{ scope.row.waiterUrl }}</template>
        </el-table-column>
        <el-table-column label="添加时间" width="180" align="center">
          <template slot-scope="scope">{{
            scope.row.createTime | formatDateTime
          }}</template>
        </el-table-column>
        <el-table-column label="操作" width="130" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleUpdata(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15, 20]"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 新增/编辑  -->
    <edit-service-dialog
      v-if="editVisible"
      :row="row"
      @handle-confirm="handleConfirm"
      @handle-cancel="editVisible = false"
    />
  </div>
</template>
<script>
import { getServiceList, deleteServiceInfo } from "@/api/config/index";
import { formatDate } from "@/utils/date";
import EditServiceDialog from "./dialogs/edit-service-dialog.vue";
import { createNamespacedHelpers } from "vuex";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "serviceList",
  components: {
    EditServiceDialog
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      row: {},
      listLoading: false,
      editVisible: false,
      typeOptions: [{ label: "全部", value: "" }]
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    }
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getList();
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleConfirm() {
      this.getList();
      this.editVisible = false;
    },

    // 添加
    handleAdd() {
      this.row = {};
      this.editVisible = true;
    },
    // 编辑
    handleUpdata(row) {
      this.row = row;
      this.editVisible = true;
    },

    //删除
    handleDelete(row) {
      this.$confirm("是否要删除客服?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteServiceInfo({ id: row.id }).then(response => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getList();
        });
      });
    },

    getList() {
      this.listLoading = true;
      getServiceList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    }
  }
};
</script>
<style lang="scss">
.success {
  color: #67c23a !important;
}
.danger {
  color: #f56c6c !important;
}
.message-container {
  height: calc(100vh - 242px);
  .el-table__body-wrapper {
    height: calc(100vh - 295px);
    overflow: auto;
  }
}
</style>
